package com.atguigu.lease.web.admin.tasks;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author zhaolingqiao
 * @CreateTime 2024/12/13
 */
@Component
public class MyTask {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    //每天凌晨一点执行定时任务，检查租约是否到期，到期则更新状态

    //@Scheduled(cron = "* * * * * *")
    @Scheduled(cron = "0 0 1 * * *")
    public void checkStatus(){
        LambdaUpdateWrapper<LeaseAgreement> leaseAgreementLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        //检查租约,status:2\5\7,是否到期，
        leaseAgreementLambdaUpdateWrapper.lt(LeaseAgreement::getLeaseEndDate,new Date());
        leaseAgreementLambdaUpdateWrapper.in(LeaseAgreement::getStatus,
                LeaseStatus.SIGNED.getCode(),
                LeaseStatus.WITHDRAWING.getCode(),
                LeaseStatus.RENEWING.getCode());
        //到期则，更新状态:已过期
        leaseAgreementLambdaUpdateWrapper.set(LeaseAgreement::getStatus,
                LeaseStatus.EXPIRED.getCode());
        leaseAgreementService.update(leaseAgreementLambdaUpdateWrapper);
    }
}
